﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BSC;
using EmrDatastructure;

namespace MvcEmrRazor.Controllers
{
    [Authorize]
    public class ProblemsController : Controller
    {
        private IEmrService _service;

        public ProblemsController()
        {
            _service = DALFactory.DataAccess.CreateDataAccessLayer();
        }
        public ActionResult Index()
        {
            return View("Index", _service.ReadListEmrProblem(System.Web.HttpContext.Current.Session["DemographicID"].ToString()));
        }


        //
        // GET: /Problems/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Problems/Create
        [HttpPost]
        public ActionResult Create(EmrProblem r)
        {
            try
            {
                r.EmrDemographicId = (string)System.Web.HttpContext.Current.Session["DemographicID"];
                _service.CreateEmrProblem(r);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /Problems/Edit/5
        public ActionResult Edit(int id)
        {
            EmrProblem r = _service.ReadEmrProblem(id.ToString());
            return View("Edit", r);
        }

        //
        // POST: /Problems/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, EmrProblem r)
        {
            try
            {
                _service.UpdateEmrProblem(r);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /Problems/Delete/5
        public ActionResult Delete(int id)
        {
            EmrProblem r = _service.ReadEmrProblem(id.ToString());
            return View("Delete", r);
        }

        //
        // POST: /Problems/Delete/5
        [ActionName("Delete")]
        [HttpPost]
        public ActionResult DeletePost(int id)
        {
            try
            {
                // TODO: Add delete logic here
                _service.DeleteEmrProblem(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }
    }
}
